package cn.biplam.back.operator.ddl.md2sql.md;

import java.sql.SQLException;
import java.util.ArrayList;

import cn.biplam.back.jda.Jda;
import cn.biplam.back.operator.ddl.md2sql.DbInfo;
import cn.biplam.common.client.DbResource;
import cn.biplam.common.paramters.SysName;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.common.wrapper.sqlresult.SqlResult;

public class MdOracle implements Md {

	/**列名信息*/
	private ArrayList<KvMap<String,String>> aColumns;
	/**主键信息*/
	private ArrayList<KvMap<String,String>> aPK;
	/**外键信息*/
	private ArrayList<KvMap<String,String>> aFKs;
	/**索引信息*/
	private ArrayList<KvMap<String,String>> aIndexs;

	private Jda mJda;
	/**表名*/
	private int iOid;
	private SqlResult rs;
	/**表所在的数据库用户*/
	private String sSchema;
	/**元数据源*/
	private DbResource mDbResource;
	/**设置是从buffer2Sql 还是 Md2sql。buffer=1，Md=2*/
	private static String sMd2SqlFrom;


	/**
	 * 功能	  初始化实例
	 * @param iOid   元数据对象id
	 * @param srcDbResource MD数据源
	 * @throws SQLException 
	 */
	@SuppressWarnings("static-access")
	public MdOracle(int iOid,DbResource mdDbResource) throws ErrorMessage{

		if(this.sSchema!=null){
			this.sSchema = sSchema.toUpperCase();
		}
		if(this.sMd2SqlFrom==null){
			this.sMd2SqlFrom = TOOLS.getValueByPropertyName(SysName.PropertiesPath,"PARSER.Md2SqlFrom");
		}
		this.iOid = iOid;
		this.aColumns = new ArrayList<KvMap<String,String>>();
		this.aFKs = new ArrayList<KvMap<String,String>>();
		this.aIndexs = new ArrayList<KvMap<String,String>>();
		this.aPK = new ArrayList<KvMap<String,String>>();
		this.mDbResource = mdDbResource;

		try {

			this.mJda = new Jda(this.mDbResource,false);
			if(this.sSchema == null && this.mDbResource.getUsername()!=null){
				this.sSchema = this.mDbResource.getUsername().toUpperCase();
			}
			if(this.iOid>0){
				this.getColumnsInfo();
				this.getPKInfo();
				this.getFKInfo();
			}
		}  catch (ErrorMessage e1) {
			ErrorMessage mErrorMessage = new ErrorMessage(e1);
			throw mErrorMessage;
		} finally{
			this.mJda.connClose();
		}

	}

	/**
	 * 功能   取表列信息
	 * @return ArrayList<KvMap<String, String>>
	 */
	public ArrayList<KvMap<String, String>> getColumns() {
		return aColumns;
	}


	/** 
	 * 功能    取主键信息
	 * @return  ArrayList<KvMap<String, String>>
	 */
	public ArrayList<KvMap<String, String>> getPK() {
		return aPK;
	}

	/** 
	 * 功能    取外键信息
	 * @return  ArrayList<KvMap<String, String>>
	 */
	public ArrayList<KvMap<String, String>> getFK() {
		return this.aFKs;
	}

	/** 
	 * 功能    取索引信息
	 * @return  ArrayList<KvMap<String, String>>
	 */
	public ArrayList<KvMap<String, String>> getIndexs() {
		return this.aIndexs;
	}

	/**
	 * 功能  从md取表信息
	 * @param sSchema   表所在的用户
	 * @param sTableName  表名，可为null，null则返回说有表
	 * @param mDbResource  源数据数据源
	 * @param mdDbResource  md数据源
	 * @return ArrayList<KvMap<String,String>>
	 * @throws ErrorMessage
	 */
	public ArrayList<KvMap<String,String>> getTable(String sSchema,String sTableName,
			DbResource mDbResource,DbResource mdDbResource) throws ErrorMessage{

		ArrayList<KvMap<String,String>> aTables = new ArrayList<KvMap<String,String>>();

		Jda mJda = new Jda(mdDbResource,false);
		try {	
			SqlResult sr = null;
			if("1".equals(sMd2SqlFrom)){
//				TableBuffer mSOB = new TableBuffer();
//				sr = mSOB.selectByDsid(sSchema, sTableName,mDbResource.getID());
			}
			KvMap<String,String> kvMap = null;
			for(int i=0;i<sr.size();i++){
				kvMap = new KvMap<String,String>();
				kvMap.put(DbInfo.TABLE_NAME, sr.get(i,DbInfo.TABLE_NAME));
				kvMap.put(DbInfo.TABLE_COMMENTS, sr.get(i,DbInfo.TABLE_COMMENTS));
				kvMap.put(DbInfo.TABLE_OID, sr.get(i,DbInfo.TABLE_OID));
				kvMap.put(DbInfo.TABLE_SCHEM, sr.get(i,DbInfo.TABLE_SCHEM));
				aTables.add(kvMap);
			}

		}catch(Exception e){
			ErrorMessage mErrorMessage = new ErrorMessage(e);
			throw mErrorMessage;
		}finally{
			mJda.connClose();
		}

		return aTables;
	}

	/**
	 * 功能   取得列信息
	 * @throws SQLException
	 * @throws ErrorMessage
	 */
	@SuppressWarnings("static-access")
	private void getColumnsInfo() throws  ErrorMessage{

		try {	
			if("1".equals(sMd2SqlFrom)){
//				SrcTableColBuffer mSOCB = new SrcTableColBuffer(this.mJda);
//				rs = mSOCB.selectByOid(iOid);
			}
			KvMap<String,String> kvMap = null;
			for(int i=0;i<rs.size();i++){
				kvMap = new KvMap<String,String>();
				kvMap.put(DbInfo.COLUMN_NAME, rs.get(i,DbInfo.COLUMN_NAME));
				kvMap.put(DbInfo.COLUMN_DATA_TYPE, rs.get(i,DbInfo.COLUMN_DATA_TYPE));
				kvMap.put(DbInfo.COLUMN_SIZE, rs.get(i,DbInfo.COLUMN_SIZE));
				kvMap.put(DbInfo.COLUMN_DECIMAL_DIGITS, rs.get(i,DbInfo.COLUMN_DECIMAL_DIGITS));
				kvMap.put(DbInfo.COLUMN_NULLABLE, rs.get(i,DbInfo.COLUMN_NULLABLE));
				kvMap.put(DbInfo.COLUMN_COMMENTS, rs.get(i,DbInfo.COLUMN_COMMENTS));
				kvMap.put(DbInfo.COLUMN_DEF, rs.get(i,DbInfo.COLUMN_DEF));
				this.aColumns.add(kvMap);
			}
		} catch (Exception e) {
			ErrorMessage mErrorMessage = new ErrorMessage(e);
			throw mErrorMessage;
		}

	}

	/**
	 * 功能   主键
	 * @throws SQLException
	 * @throws ErrorMessage
	 */
	private void getPKInfo() throws ErrorMessage{

		try {
			if("1".equals(sMd2SqlFrom)){
//				PkBuffer mPb = new PkBuffer(this.mJda);
//				rs = mPb.selectByOid(iOid);
			}
			KvMap<String,String> kvMap = null;
			for(int i=0;i<rs.size();i++){
				kvMap = new KvMap<String,String>();
				kvMap.put(DbInfo.PK_COLUMN_NAME, rs.get(i,DbInfo.PK_COLUMN_NAME));
				kvMap.put(DbInfo.PK_NAME, rs.get(i,DbInfo.PK_NAME));
				this.aPK.add(kvMap);
			}
		} catch (Exception e) {
			ErrorMessage mErrorMessage = new ErrorMessage(e);
			throw mErrorMessage;
		}

	}

	/**
	 * 功能   外键
	 * @throws SQLException
	 * @throws ErrorMessage
	 */
	private void getFKInfo() throws ErrorMessage{

		try {
			if("1".equals(sMd2SqlFrom)){
//				FkTableBuffer mFB = new FkTableBuffer(this.mJda);
//				rs = mFB.selectByOid(iOid);
			}
			KvMap<String,String> kvMap = null;
			for(int i=0;i<rs.size();i++){

				kvMap = new KvMap<String,String>();
				kvMap.put(DbInfo.FK_FK_NAME, rs.get(i,DbInfo.FK_FK_NAME));
				kvMap.put(DbInfo.FK_FKCOLUMN_NAME, rs.get(i,DbInfo.FK_FKCOLUMN_NAME));
				kvMap.put(DbInfo.FK_PKCOLUMN_NAME, rs.get(i,DbInfo.FK_PKCOLUMN_NAME));
				kvMap.put(DbInfo.FK_PKTABLE_NAME, rs.get(i,DbInfo.FK_PKTABLE_NAME));
				this.aFKs.add(kvMap);
			}
		} catch (Exception e) {
			ErrorMessage mErrorMessage = new ErrorMessage(e);
			throw mErrorMessage;
		}

	}

}
